﻿#pragma once

#include "Common/MessageDispatch/MessageDispatch.h"
#include "Common/AccountDefine.h"
#include "Common/ActivityDefine.h"
#include "Common/EventDefine.h"
#include "Common/Event/Event.h"
#include "base/core/object_pool.h"
#include "Common/Timer/TimerAxis.h"

class GActivity;
class GActivityGoblin;
class GActivityAncientElf;
class GActivityWorldBoss;
class GActivitySonOfKilling;
class GActivityChaosDragon;
class GActivityLostCity;
class GActivityMonsAttackCity;

class LogicActivityModule : 
					public IClientMessageHandler, 
					public IServerMessageHandler,
					public ITimerHandler,
					public IEventExecuteSink,
					public ManualSingleton<LogicActivityModule>
{
	typedef std::map<uint32_t, GActivity*> StaticActivityMap;
	typedef std::unordered_map<uint32_t, ActData> ActDataMap;
public:
	bool Init();
	void UnInit();

	////////////////////////////// IClientMessageHandler /////////////////////////////
	virtual void		onClientMessage(uint32_t actionId, RECV_PACKAGE &package, uint64_t Id /* = 0 */);
	virtual const char* getClientHandlerDebugInfo(void) { return "LogicActivityModule::onClientMessage"; }

	//////////////////////////////  IServerMessageHandler ////////////////////////////////////////////
	virtual void		onServerMessage(uint32_t actionId, RECV_PACKAGE &package);
	virtual const char* getServerHandlerDebugInfo(void){ return "LogicActivityModule::onServerMessage"; }

	//////////////////////////////// ITimerHandler //////////////////////////////////////////
	virtual void OnTimer(uint32_t nTimerID);
	////////////////////////////////  IEventExecuteSink //////////////////////////////////////////
	virtual void		OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen){};

	bool				Update(uint64_t tick);

	//////////////////////////////////////////////////////////////////////////

	GActivity *MakeActivityObj(uint32_t activityId);
	void RecycleActivity(GActivity *pActivity);

	GActivity* MutStaticActivity(uint32_t activityId);
	void UpdateActivity();
	void CloseStaticActivity(uint32_t activityId);
	GActivity* FindStaticActivity(uint32_t activityId);
	bool IsRunning(uint32_t actId);
	bool IsRunningByType(ACTIVITY_TYPE_ID enType);
	//获取活动数据(仅给内部用)
	const ActData*GetActData(uint32_t actId);
	//时间是否再指定活动期间
	bool IsDuringAct(uint32_t actId, uint64_t lastTime);
	//世界答题活动，答正确一题给的奖励
	bool RewardActiveAnswerOneSucceed(Player *p);
	//每日服答题活动,答正确一题给的奖励
	bool RewardActiveDailyAnswerOneSucceed(Player * pPlayer);
	//添加活动(此接口用于再逻辑服没有创建活动对象的情况）
	void OnAttendActivity(uint32_t activityId, CharIDType cid);
	//完成活动(此接口用于再逻辑服没有创建活动对象或者完成时 活动对象已经回收了的情况)
	void OnFinishActivity(uint32_t activityId, CharIDType cid);
	//接取活动收集任务
	void OnAcceptActCollectMission(CharIDType cid);
private:
	//中心服通知逻辑服静态地图活动开启
	bool				OnStaticActivityOpen(RECV_PACKAGE &package);
	//中心服通知逻辑服静态地图活动关闭
	bool				OnStaticActivityClose(RECV_PACKAGE &package);
	//中心服通知逻辑服加活动奖励
	bool				OnStaticActivityReward(RECV_PACKAGE &package);
	//中心服通知逻辑服活动数据
	bool				OnNotifyActData(RECV_PACKAGE &package);
	//中心服通知逻辑服答题活动结束
	bool				OnNotifyActiveAnswerEnd(RECV_PACKAGE &package);
	//中心服通知逻辑服运营设置
	bool				OnOperateSet(RECV_PACKAGE &package);
	//中心服通知逻辑服角色完成活动
	bool				OnActivityJoin(RECV_PACKAGE &package);
public:
	uint32_t			GetSonOfKillingMonsterId();
	uint32_t			GetSonOfKillingExtraDropId();
	//能否攻击活动怪物
	bool				BossCanAttack(Player *p, CharIDType monsterCid);
	//能否加入仇恨列表(公会战成员)
	bool				CanAddHateList(Player *killer, Player *deader);
	//杀戮之子是否pk免惩罚
	bool				RemitSonPKPunish(uint32_t sceneId);
	//能否使用道具传送
	bool				CanTransScene(Player *p);
	//死亡是否能掉落
	bool				CanDeadDrop(Player *p);
	//活动过程加入公会
	void				OnJoinUnion(Player *p);
	//活动过程退出公会
	void				OnQuitUnion(Player *p);
	//活动过程断线
	void				OnDisconnect(Player *p);

	////////////////////////////////////////////////////////////////////////////////////////////////
	//哥布林LUA配置
	bool LuaGetGoblinCfg(GoblinCfg &cfg, int32_t valueLev);
	//远古精灵LUA配置
	bool LuaGetAncientElfCfg(AncientElfCfg &cfg,int32_t collegaLev);
	//获取远古精灵答题正确之后的奖励
	void GetEruditeElfReward(int32_t level, int32_t &gold, int32_t &unionContri);
	//怪物攻城LUA配置
	bool LuaGetMonsAttackCity(MonsAttackCityCfg &cfg);

	///////////////////////////////  活动追踪数据通知 ///////////////////////////////////////////
	//通知增加追踪数据
	void NotifyAddTrack(SET_UINT64 &setPlayer, int8_t type, MultiTrackProto &trackProto);
	//通知更新追踪数据
	void NotifyUpdateTrack(SET_UINT64 &setPlayer, int8_t type, MultiTrackProto &trackProto);
	//通知移除追踪数据
	void NotifyDelTrack(SET_UINT64 &setPlayer, int8_t type);

	//------------------答题活动----------------------------------------
	bool OnGetWorldAnswerInfo(CharIDType charID, ActivityAnswerInfoReq & req);   //世界活动答题信息请求
	bool OnGetDailyAnswerInfo(CharIDType charID, ActivityAnswerInfoReq & req);	 //每日活动答题信息请求
	bool OnWorldAnswerResult(CharIDType charID, ActivityAnswerResultReq & req);	 //世界活动答题结果
	bool OnDailyAnswerResult(CharIDType charID, ActivityAnswerResultReq & req);  //每日活动答题结果
	bool OnWorldAnswerGetLastReward(CharIDType charID, ActivityAnswerGetLastRewardReq & req);	//世界活动领取最终全对奖励
	bool OnDailyAnswerGetLastReward(CharIDType charID, ActivityAnswerGetLastRewardReq & req);   //每日活动领取最终全对奖励
	//------------------------------------------------------------------


private:
	void				DailyUpdate(RECV_PACKAGE &package);
	void				WeekUpdate(RECV_PACKAGE &package);
	void				DailyZeroUpdate(RECV_PACKAGE &package);
	void				WeekZeroUpdate(RECV_PACKAGE &package);

private:
	bool				OnEnterActivityReq(CharIDType charID, RECV_PACKAGE &package);
	bool				OnLeaveActivityReq(CharIDType charID, RECV_PACKAGE &package);
	bool				OnNpcTalkReq(CharIDType charID, RECV_PACKAGE &package);
	bool				OnLostCitySend(CharIDType charID, RECV_PACKAGE &package);
	bool				OnGetSonTrackInfo(CharIDType charID, RECV_PACKAGE &package);
	bool				OnSonPosInfoReq(CharIDType charID, RECV_PACKAGE &package);

//答题系统
	bool				OnGetAnswerInfo(CharIDType charID, RECV_PACKAGE &package);
	bool				OnAnswerResult(CharIDType charID, RECV_PACKAGE &package);
	bool				OnAnswerGetLastReward(CharIDType charID, RECV_PACKAGE &package);
public:
	LogicActivityModule();
	virtual ~LogicActivityModule();

private:
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	ObjectPool<GActivity>*						_activityPool;
	ObjectPool<GActivityGoblin>*				_activityGoblinPool;
	ObjectPool<GActivityAncientElf>*			_activityAncientElfPool;
	ObjectPool<GActivityWorldBoss>*				_activityWorldBossPool;
	ObjectPool<GActivitySonOfKilling>*			_activitySonOfKillingPool;
	ObjectPool<GActivityChaosDragon>*			_activityChaoDragonPool;
	ObjectPool<GActivityLostCity>*				_activityLostCityPool;
	ObjectPool<GActivityMonsAttackCity>*		_activityMonsAttackCityPool;
	//静态地图挂载的活动
	StaticActivityMap m_mapActivity;
	//记录活动状态，中心服活动状态改变通知所有的逻辑服，目前主要是任务用到 activityid - ActData
	ActDataMap m_mapActivityData;


};

#define g_MakeGActivityMgr() (LogicActivityModule::Instance())
#define g_GetGActivityMgr()  (LogicActivityModule::GetInstance())
#define g_DelGActivityMgr()  (LogicActivityModule::Destroy())